<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let p1 = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve(111)
                // reject(111)
            }, 1000);
        })

        let p2 = new Promise((resolve, reject) => {
            setTimeout(() => {
                // resolve(222)
                reject("error");
            }, 3000);
        })

        let p3 = new Promise((resolve, reject) => {
            setTimeout(() => {
                // resolve(333)
                reject(111)
            }, 2000);
        })

        // 封装自己的promise.allSettled方法[{status:"fulfilled",value:111},{reson:111,status:"rejected"}]
        function myAllSettled(promises) {
            let result = []
            // 计数器
            let count = 0
            return new Promise(resolve => {
                // 处理每一项promise结果的函数
                function addPromiseResults(status, value, index) {
                    if (status === 'fulfilled') {
                        result[index] = {
                            status,
                            value
                        }
                    } else {
                        result[index] = {
                            status,
                            reason: value
                        }
                    }
                    count++
                    if (count === promises.length) resolve(result)
                }
                // 遍历每一项
                promises.forEach((promise, index) => [
                    promise.then(res => {
                        addPromiseResults('fulfilled', res, index)
                    }, err => {
                        addPromiseResults('rejected', err, index)
                    })
                ])
            })
        }
        // 调用
        // myAllSettled([p1, p2, p3]).then(res => {
        //     console.log(res);
        // })


        // 封装自己的promise.all方法
        function myAll(promises) {
            // 计数器
            let count = 0
            let result = []
            return new Promise(resolve => {
                // 处理每一项promise结果的函数
                function addPromiseResults(status, value, index) {
                    if (status === 'fulfilled') {
                        result[index] = {
                            status,
                            value
                        }
                    } else {
                        result[index] = {
                            status,
                            reason: value
                        }
                    }
                    count++
                    if (count === promises.length) resolve(result)
                }
                // 遍历每一项
                promises.forEach((promise, index) => {
                    promise.then(res => {
                        addPromiseResults('fulfilled', res, index)
                    }, err => {
                        // 遇到错误直接退出
                        resolve({
                            reason: err,
                            status: 'rejected'
                        })
                    })
                })
            })
        }
        // 调用
        // myAll([p1, p2, p3]).then(res => {
        //     console.log(res);
        // })


        // 封装自己的promise.any方法
        // 如果有一个成功则返回成功结果(不是数组，是一项)
        // 所有的都失败则报错
        function myAny(promises) {
            let count = 0
            return new Promise((resolve, reject) => {
                // 遍历每一项
                promises.forEach(promise => {
                    promise.then(res => {
                        // 有一个成功则返回改成功结果
                        resolve({
                            status: 'fulfilled',
                            value: res
                        })
                    }, err => {
                        count++
                        if (count === promises.length)
                            reject(new AggregateError('All promises were rejected'))
                    })
                })
            })
        }
        // 调用
        myAny([p1, p2, p3]).then(res => {
            console.log(res);
        }, err => {
            console.log(err);
        })


        // 封装自己的promise.race方法
        function myRace(promises) {
            return new Promise(resolve => {
                // 遍历每一项
                promises.forEach(promise => {
                    promise.then(res => {
                        // 遇到成功直接退出
                        resolve({
                            status: 'fulfilled',
                            value: res
                        })
                    }, err => {
                        // 遇到错误直接退出
                        resolve({
                            reason: err,
                            value: err
                        })
                    })
                })
            })
        }
        // 调用
        // myRace([p1, p2, p3]).then(res => {
        //     console.log(res);
        // })
        new Promise((resolve, reject) => {
            throw new Error('出错了')
        }).then(res => {
            console.log(res);
        }, err => {
            console.log(err);
        })
    </script>
</body>

</html>